package com.lin.mr.maizilaundry.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.utils.CountEvent;
import com.lin.mr.maizilaundry.utils.ShareUtils;

import de.greenrobot.event.EventBus;

public class RegisterActivity extends Activity implements View.OnClickListener {
    private ImageView btn_back_register;
    private EditText register_name;
    private RadioGroup register_rg;
    private RadioButton btn_male;
    private RadioButton btn_female;
    private EditText register_phone_number;
    private TextView address;
    private TextView address_register;
    private Spinner spinner_register;
    private ImageView spinner_arrow;
    private Button save_register;

    private String city;
    private String gender;
    private ArrayAdapter<String> adapter;
    private static final String[] cities = {"北京", "上海", "广州", "深圳", "成都", "重庆", "杭州", "天津"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();

        register_name.setText(ShareUtils.getName(RegisterActivity.this));
        register_phone_number.setText(ShareUtils.getNumber(RegisterActivity.this));

        address_register.setText(ShareUtils.getAddress(RegisterActivity.this));

        /**
         * 给Spinner设置上次选中的值
         */
        SpinnerAdapter spinnerAdapter = spinner_register.getAdapter();
        for (int i = 0; i < spinnerAdapter.getCount(); i++) {
            //遍历Spinner中的选项，如果某一项与之前保存的值相同，就将其设置为选中状态
            if (spinnerAdapter.getItem(i).equals(ShareUtils.getCity(RegisterActivity.this))){
                spinner_register.setSelection(i,true);
            }
        }

        /**
         * 给RadioGroup设置上一次选中的值
         */
        if (ShareUtils.getGender(RegisterActivity.this).equals("先生")){
            btn_male.setChecked(true);
        }else {
            btn_female.setChecked(true);
        }

    }

    private void initViews() {
        btn_back_register = (ImageView) findViewById(R.id.btn_back_register);
        register_name = (EditText) findViewById(R.id.register_name);
        register_rg = (RadioGroup) findViewById(R.id.register_radioGroup);
        btn_male = (RadioButton) findViewById(R.id.male);
        btn_female = (RadioButton) findViewById(R.id.female);
        register_phone_number = (EditText) findViewById(R.id.register_phone_number);
        address = (TextView) findViewById(R.id.address);
        address_register = (TextView) findViewById(R.id.address_register);
        spinner_register = (Spinner) findViewById(R.id.spinner_register);
        spinner_arrow = (ImageView) findViewById(R.id.spinner_arrow);
        save_register = (Button) findViewById(R.id.save_register);

        btn_back_register.setOnClickListener(this);
        save_register.setOnClickListener(this);
        btn_male.setOnClickListener(this);
        btn_female.setOnClickListener(this);

        //将下拉列表中的内容与ArrayAdapter关联起来
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, cities);
        //设置下拉列表的风格
        adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);

        spinner_register.setAdapter(adapter);
        //spinner的点击事件
        spinner_register.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //获取spinner选中的值
                city = (String) spinner_register.getSelectedItem();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        register_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.male:
                        gender = "先生";
                        break;
                    case R.id.female:
                        gender = "女士";
                        break;
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.save_register:
                String name = register_name.getText().toString();
                String phoneNumber = register_phone_number.getText().toString();
                String address = address_register.getText().toString();


                //判断用户是否填写完毕
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phoneNumber) || TextUtils.isEmpty(address)){
                    Toast.makeText(this, "请填写完整信息!", Toast.LENGTH_SHORT).show();
                }else {
                    //在这里提交用户信息可以避免保存为空字符串
                    ShareUtils.putName(this,name);
                    ShareUtils.putGender(this,gender);
                    ShareUtils.putNumber(this,phoneNumber);
                    ShareUtils.putCity(this,city);
                    ShareUtils.putAddress(this,address);

                    EventBus.getDefault().post(new CountEvent(7));//让FragmentMore更新名字和手机号
//                    EventBus.getDefault().post(new CountEvent(6));//HomeActivity刷新界面
                    HomeActivity.viewPager_home.setCurrentItem(0);
                    HomeActivity.viewPager_home.setCurrentItem(2);
//
                    RegisterActivity.this.finish();
                    Toast.makeText(this, "你的用户信息已经保存", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_back_register:
                RegisterActivity.this.finish();
                break;
        }
    }

}
